home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-17 | 5.3 KB | 196 lines |
- // TextureTest.java
- //
- // Copyright (C) 1995 Dimension X, Inc.
- // Chris Laurel 10-28-95
- //
- // This code may be redistributed freely.
-
- import java.applet.Applet;
- import java.awt.*;
- import java.net.URL;
- import java.net.MalformedURLException;
- import ice.*;
-
-
- public class TextureTest extends Applet
- {
- Graphics3D g3d;
- Model cube;
- float xangle, yangle;
- int lastX, lastY;
- Matrix4 t = new Matrix4();
- Texture tex;
-
-
- public void init()
- {
- cube = createBox(2, 2, 2);
- }
-
- public void start()
- {
- createGraphicsContext();
- if (tex == null) {
- try {
- Image texImage = getImage(new URL(getDocumentBase(), getParameter("image")));
- tex = new Texture(g3d, texImage.getSource(), Texture.RGB);
- } catch (MalformedURLException e) {
- System.out.println("Bad URL");
- }
- }
-
- display();
- }
-
- public synchronized void display()
- {
- // Clear the contents of the frame buffer
- g3d.clear(Graphics3D.ColorBuffer);
-
- g3d.useTexture(tex);
-
- // Set up the model transformation
- t.identity();
- // rotate it
- t.xrotate(xangle);
- t.yrotate(yangle);
- // translate the cube away from the camera along the z axis
- t.translate(0, 0, -5);
-
- // render
- g3d.concatModelMatrix(t);
- g3d.renderSolid(cube);
- g3d.popModelMatrix();
-
- repaint();
- }
-
-
- // Standard applet methods
- public boolean mouseDown(Event e, int x, int y)
- {
- lastX = x;
- lastY = y;
- return true;
- }
-
- public boolean mouseDrag(Event e, int x, int y)
- {
- yangle += (float) (x - lastX) / (float) size().width * (float) Math.PI;
- xangle += (float) (y - lastY) / (float) size().height * (float) Math.PI;
- lastX = x;
- lastY = y;
- display();
- return true;
- }
-
- public void paint(Graphics g)
- {
- update(g);
- }
-
- public void update(Graphics g)
- {
- if (g3d != null)
- g3d.paint(g, 0, 0);
- }
-
-
- private void createGraphicsContext()
- {
- // Create a new 3D graphics context . . . since we're just
- // going to be rendering a cube, we don't need a depth
- // buffer--all necessary hidden surface removal is handled
- // by backface culling. Note that we need to have a
- // regular awt Graphics object in order to create a new
- // 3D graphics context (this will likely change once
- // Iced Java is ported to the beta JDK.)
- g3d = new Graphics3D(this,
- Graphics3D.ColorBuffer,
- size().width, size().height);
-
- // Set the clear color to be light gray (generally, the same
- // color as the main browser window)
- g3d.setClearColor(Color.lightGray);
-
- g3d.disableLighting();
- g3d.enableTexturing();
- // g3d.setTextureMode(Graphics3D.TextureModeModulate);
-
- // Enable dithering, if necessary. This will only have an effect
- // for a non-truecolor display.
- g3d.enableDithering();
- }
-
-
- // Create a box model centered about the origin.
- Model createBox(int width, int height, int depth)
- {
- try {
- // Create a new, empty model with per vertex materials,
- // no normals (we're just using emissiveColor so we
- // don't need them for lighting), and no texture
- // coordinates;
- Model m = new Model(Model.BindingPerVertex,
- Model.BindingNone,
- true);
-
- // Add the vertices
- m.addVertex(-width / 2, -height / 2, depth / 2);
- m.addVertex(-width / 2, height / 2, depth / 2);
- m.addVertex(-width / 2, height / 2, -depth / 2);
- m.addVertex(-width / 2, -height / 2, -depth / 2);
- m.addVertex(width / 2, -height / 2, depth / 2);
- m.addVertex(width / 2, height / 2, depth / 2);
- m.addVertex(width / 2, height / 2, -depth / 2);
- m.addVertex(width / 2, -height / 2, -depth / 2);
-
- // Add the materials
- Material mat = new Material(Color.white, Color.black,
- Color.black, Color.black,
- 1, 1);
-
- mat.emissive = Color.blue; m.addMaterial(mat);
- mat.emissive = Color.red; m.addMaterial(mat);
- mat.emissive = Color.green; m.addMaterial(mat);
- mat.emissive = Color.white; m.addMaterial(mat);
- mat.emissive = Color.cyan; m.addMaterial(mat);
- mat.emissive = Color.yellow; m.addMaterial(mat);
- mat.emissive = Color.magenta; m.addMaterial(mat);
- mat.emissive = Color.gray; m.addMaterial(mat);
-
- // Add the texture coordinates
- m.addTextureCoord(0, 0);
- m.addTextureCoord(1, 0);
- m.addTextureCoord(1, 1);
- m.addTextureCoord(0, 1);
-
- // Add the faces. vi is an array which will contain the
- // vertex indices for each face. Since each vertex is
- // mapped to one material, we can use the same array
- // for the material indices.
- int vi[] = new int[4];
- int ti[] = new int[4];
- ti[0] = 0; ti[1] = 1; ti[2] = 2; ti[3] = 3;
- vi[0] = 0; vi[1] = 1; vi[2] = 2; vi[3] = 3;
- m.polygon(4, vi, vi, null, ti); // left (-X)
- vi[0] = 7; vi[1] = 6; vi[2] = 5; vi[3] = 4;
- m.polygon(4, vi, vi, null, ti); // right (+X)
- vi[0] = 4; vi[1] = 5; vi[2] = 1; vi[3] = 0;
- m.polygon(4, vi, vi, null, ti); // front (+Z)
- vi[0] = 3; vi[1] = 2; vi[2] = 6; vi[3] = 7;
- m.polygon(4, vi, vi, null, ti); // back (-Z)
- vi[0] = 1; vi[1] = 5; vi[2] = 6; vi[3] = 2;
- m.polygon(4, vi, vi, null, ti); // top (+Y)
- vi[0] = 3; vi[1] = 7; vi[2] = 4; vi[3] = 0;
- m.polygon(4, vi, vi, null, ti); // bottom (-Y)
-
- return m;
- } catch (InvalidModelException e) {
- return null;
- }
-
- }
-
- }
-